# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=rnp
pkgver=0.17.0
pkgrel=0
pkgdesc="High performance C++ OpenPGP library used by Mozilla Thunderbird"
url="https://www.rnpgp.org/"
arch="all"
license="BSD-2-Clause"
checkdepends="gnupg gtest-dev"
makedepends="
	asciidoctor
	botan-dev
	bzip2-dev
	cmake
	json-c-dev
	sexpp-dev
	zlib-dev
	"
subpackages="$pkgname-dev $pkgname-doc librnp"
source="https://github.com/rnpgp/rnp/archive/v$pkgver/rnp-$pkgver.tar.gz
	cleanup-nopermsdir.patch
	system-libsexpp.patch
	"

case "$CARCH" in
	# FIXME: one test fails
	s390x) options="!check";;
esac

build() {
	# Avoid polluting /tmp.
	export TEMP="$builddir/.tmp"
	mkdir -p "$TEMP"

	local crossopts=
	if [ "$CBUILD" != "$CHOST" ]; then
		crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	# CMAKE_INTERPROCEDURAL_OPTIMIZATION - enable LTO
	cmake -B build \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DBUILD_SHARED_LIBS=ON \
		-DBUILD_TESTING="$(want_check && echo ON || echo OFF)" \
		-DDOWNLOAD_GTEST=OFF \
		-DDOWNLOAD_RUBYRNP=OFF \
		-DGIT_EXECUTABLE=/bin/false \
		-DSYSTEM_LIBSEXPP=ON \
		$crossopts .
	cmake --build build
}

check() {
	# cli_tests-Encryption - fails on x86_64, but passes on all others (probably broken test)
	# cli_tests-Misc - probably incompatibility with recent gnupg
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest -j1 \
		--exclude-regex '(cli_tests-Encryption|cli_tests-Misc|test_ffi_set_log_fd)$'
}

package() {
	pkgdesc="$pkgdesc (CLI tools)"

	DESTDIR="$pkgdir" cmake --install build
}

librnp() {
	default_libs
}

sha512sums="
0aa5262aa3be163b8a8931d6b9200ced52bb640cd7b02b50009cfb8d5fe1e664d27b2c4a0a3e534cf59d6ea42fbc1c62c970a07cadc27ab6962e0f69f996f74d  rnp-0.17.0.tar.gz
abb84713d54763b5cfb4a3a80078db04ef3ad7c8913bfdcc44c82081e1d4547039e78cfaae8a11ead53e05bc7f9308641c044a62c8fe9f8c4922b12495c61240  cleanup-nopermsdir.patch
379ad237dc7db9d6b56e48cd3f8d826fdeb63ac5271eab5d96f0608fa445b370fb0624a04c86216e6a8e27a576d5a327fd20e43f5356ca699e2d608c6519f0a9  system-libsexpp.patch
"
